package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.Experiment;
import Entity.Selection;
import Service.ExperimentService;
import Service.SelectionService;

public class MyExpServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MyExpServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		
		String op=request.getParameter("op");
		if(op!=null&&op!=""&&op.equals("detail")){
			String url=request.getParameter("url");//url eid Expid ExpTe_num ExpTe_name name
			String eid=request.getParameter("eid");
			String id=request.getParameter("Expid");
			int Expid=-1;
			if(id!=null&&id!="") Expid=Integer.parseInt(id);
			SelectionService ser=new SelectionService();
			List<Selection> list=ser.findByEx_id(Expid);
			int num=0;//报名人数
			if(list!=null) num=list.size();
			String ExpTe_num=request.getParameter("ExpTe_num");
			String ExpTe_name=request.getParameter("ExpTe_name");
			String name=request.getParameter("name");
			request.setAttribute("url", url);
			request.setAttribute("list", list);
			request.setAttribute("tid",ExpTe_num);
			request.setAttribute("tname",ExpTe_name);
			request.setAttribute("name",name);
			request.setAttribute("eid", eid);
			request.setAttribute("num",String.valueOf(num));
			request.getRequestDispatcher("/WEB-INF/page/ExpDetail.jsp").forward(request,response);
			return;
		}else if(op!=null&&op!=""&&op.equals("quit")){
			request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request,response);
			return;
		}
		else if(op!=null&&op!=""&&op.equals("setScore")){
			String url=request.getParameter("url");//url eid Expid ExpTe_num ExpTe_name name
			String eid=request.getParameter("eid");
			String id=request.getParameter("Expid");
			String ExpTe_num=request.getParameter("ExpTe_num");
			String ExpTe_name=request.getParameter("ExpTe_name");
			String name=request.getParameter("name");
			String sid=request.getParameter("sid");
			String score=request.getParameter("score");
			SelectionService ser=new SelectionService();
			int ssid=0,grade=0;
			if(score!=null&&score!="") grade=Integer.parseInt(score);
			if(sid!=null&&sid!="") ssid=Integer.parseInt(sid);
			ser.setGrade(ssid, grade);

			int Expid=-1;
			if(id!=null&&id!="") Expid=Integer.parseInt(id);
			List<Selection> list=ser.findByEx_id(Expid);
			int num=0;//报名人数
			if(list!=null) num=list.size();
			request.setAttribute("url", url);
			request.setAttribute("list", list);
			request.setAttribute("tid",ExpTe_num);
			request.setAttribute("tname",ExpTe_name);
			request.setAttribute("name",name);
			request.setAttribute("eid", eid);
			request.setAttribute("num",String.valueOf(num));
			request.getRequestDispatcher("/WEB-INF/page/ExpDetail.jsp").forward(request,response);
			return;
		}
		else if(op!=null&&op!=""&&op.equals("add1")){
			String tid=request.getParameter("tid");
			String tname=request.getParameter("tname");
			request.setAttribute("tid", tid);
			request.setAttribute("tname", tname);
			request.getRequestDispatcher("/WEB-INF/page/AddExp.jsp").forward(request, response);
			return;
		}else if(op!=null&&op!=""&&op.equals("add2")){
			String tid=request.getParameter("tid");
			String tname=request.getParameter("tname");
			String ename=request.getParameter("ename");
			String beg=request.getParameter("beg");
			int begT=0;
			if(beg!=null&&beg!="")begT=Integer.parseInt(beg);
			String end=request.getParameter("end");
			int endT=0;
			if(end!=null&&end!="")endT=Integer.parseInt(end);
			String type=request.getParameter("type");
			int typeT=0;
			if(type!=null&&type!="")typeT=Integer.parseInt(type);
			ExperimentService ser=new ExperimentService();
			Experiment exp=new Experiment();
			exp.setBegweek(begT);exp.setEndweek(endT);exp.setName(ename);exp.setTe_name(tname);exp.setTe_number(tid);exp.setType(typeT);
			ser.addExperiment(exp);
			
			
			ExperimentService ExpSer=new ExperimentService();
			SelectionService SSer=new SelectionService();
			List<Experiment> ExpList=ExpSer.findAllExperiment();
			List<Experiment> list=new ArrayList<Experiment>();
			List<Integer> num=new ArrayList<Integer>();
			for(int i=0;i<ExpList.size();i++){
				String d=ExpList.get(i).getTe_number();
				if(tid!=null&&tid!=""&&tid.equals(d)){
					int n=0;
					int id=ExpList.get(i).getId();
					list.add(ExpList.get(i));
					List<Selection> tmp=SSer.findByEx_id(id);
					if(tmp!=null) n=tmp.size();
					num.add(n);
				}
				else continue;
				//if(tid==null||tid==""||(!tid.equals(ExpList.get(i).getTe_number()))) continue;
			}
			//num[i]对应第i个实验ExpList[i]的数量
			request.setAttribute("list", list);
			request.setAttribute("num", num);
			request.setAttribute("tname", tname);
			request.setAttribute("tid", tid);
			request.getRequestDispatcher("/WEB-INF/page/MyExp.jsp").forward(request,response);
			return;
		}
		
		//else if(op!=null&&op!=""&&op.equals("ret")){
//			String tid=request.getParameter("tid");
//			String tname=request.getParameter("tname");
//			request.setAttribute("tid", tid);
//			request.setAttribute("tname", tname);
//			request.getRequestDispatcher("/WEB-INF/page/MyExp.jsp").forward(request,response);
//		}
		else{
		
		String tid=request.getParameter("tid");
		String tname=request.getParameter("tname");
		System.out.print(tid);
		ExperimentService ExpSer=new ExperimentService();
		SelectionService SSer=new SelectionService();
		List<Experiment> ExpList=ExpSer.findAllExperiment();
		List<Experiment> list=new ArrayList<Experiment>();
		List<Integer> num=new ArrayList<Integer>();
		for(int i=0;i<ExpList.size();i++){
			String d=ExpList.get(i).getTe_number();
			if(tid!=null&&tid!=""&&tid.equals(d)){
				int n=0;
				int id=ExpList.get(i).getId();
				list.add(ExpList.get(i));
				List<Selection> tmp=SSer.findByEx_id(id);
				if(tmp!=null) n=tmp.size();
				num.add(n);
			}
			else continue;
			//if(tid==null||tid==""||(!tid.equals(ExpList.get(i).getTe_number()))) continue;
		}
		//num[i]对应第i个实验ExpList[i]的数量
		request.setAttribute("list", list);
		request.setAttribute("num", num);
		request.setAttribute("tname", tname);
		request.setAttribute("tid", tid);
		request.getRequestDispatcher("/WEB-INF/page/MyExp.jsp").forward(request,response);
		}
		
		
//		List<Integer>num=new ArrayList<Integer>();
//		String tid=request.getParameter("tid");
//		String tname=request.getParameter("tname");
//		String eid=request.getParameter("eid");
//		ExperimentService ser=new ExperimentService();
//		SelectionService s=new SelectionService();
//		List<Experiment> l=ser.findAllExperiment();
//		List<Experiment>list=new ArrayList<Experiment>();
//		if(l!=null){
//			for(int i=0;i<l.size();i++){
//				String tmp=l.get(i).getTe_number();
//				if(tmp!=null&&tmp!=""&&tmp.equals(tid)){
//					list.add(l.get(i));
//					if(eid!=null&&eid!=""){
//					List<Selection> tmp1=null;
//					tmp1=s.findByEx_id(Integer.parseInt(eid));
//					num.add(tmp1.size());
//					}
//				}
//			}
//		}
//		//List<Selection> slist=s.findByEx_id(eid);
//		request.setAttribute("list", l);
//		request.setAttribute("tid", tid);
//		request.setAttribute("tname", tname);
//		request.setAttribute("num", num);
//		request.getRequestDispatcher("/WEB-INF/page/MyExp.jsp").forward(request, response);
//		
		
		
		
		
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the GET method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
